package com.lmj.controller;

import com.lmj.domain.Account;
import com.lmj.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/account")
public class AccountController {

    //account封装完毕要传递用到serverce,所以注入serverce
    //Could not autowire. No beans of 'AccountService' type found.要去加@Serverce
    @Autowired
    private AccountService accountService;
    //保存
    //可以指定响应数据的数据类型，同时指定编码
    @RequestMapping(value = "/save",produces ="text/html;charset=UTF-8")
    @ResponseBody()
      public String save(Account account){

        accountService.save(account);
        return "保存成功";
      }

    //查询
    @RequestMapping("/findAll")
      public ModelAndView findAll(){

          List<Account> accountList = accountService.findAll();
           ModelAndView modelAndView=new ModelAndView();
           modelAndView.addObject("acccountList",accountList);
           modelAndView.setViewName("accountList");//视图解析器找的是谁？写谁Web-info下的pages下的accountList
          return modelAndView;
      }

}
